package com.huawei.hmsdemo10086.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.huawei.hmsdemo10086.POJO.Evaluate;
import com.huawei.hmsdemo10086.R;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class EvaluateActivity extends AppCompatActivity {

    private SharedPreferences sharedPreferences;
    Button submit;
    TextView content;
    private ImageView goods_img;
    private TextView goods_name;

    int goodsid;
    int orderid;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluate);
        Intent intent = this.getIntent();
        submit = (Button) findViewById(R.id.submit);
        content = (TextView)findViewById(R.id.evaluate_content);
        goods_name = (TextView)findViewById(R.id.goods_title) ;
        goods_img = (ImageView)findViewById(R.id.goods_img);
        goods_name.setText(intent.getStringExtra("goods_name"));

        Glide.with(this).load(intent.getStringExtra("good_img_src")).into(goods_img);
        goodsid = intent.getIntExtra("goodsid",-1);
        orderid = intent.getIntExtra("orderid",-1);;
        sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        userId = sharedPreferences.getString("userId",null);

        TextView mTitleTextView = (TextView) this.findViewById(R.id.text_title);
        mTitleTextView.setText("评价商品");
        Button back = (Button)this.findViewById(R.id.button_backward);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EvaluateActivity.this,CarActivity.class);
                startActivity(intent);
                finish();
            }
        });


        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save_evaluate();
                Intent intent = new Intent(EvaluateActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }

    public void save_evaluate()
    {


        System.out.println(orderid);
        String url = "http://10.0.2.2:8443/api/insertGoodsEvaluate";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        String json = "{\n" +
                "  \"userid\": \"" + userId + "\",\n" +
                "  \"orderid\": \"" + orderid + "\",\n" +
                "  \"message\": \"" + content.getText().toString() + "\",\n" +
                "  \"goodsid\": \"" + goodsid + "\"\n" +
                "}";


        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("1111111111");
            }
        });
    }
}